package com.example.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.entity.*;
import com.example.springboot.entity.vo.AssignParm;
import com.example.springboot.entity.vo.AssignVo;
import com.example.springboot.mapper.StudentMapper;
import com.example.springboot.mapper.SysMenuMapper;
import com.example.springboot.mapper.SysRoleMapper;
import com.example.springboot.mapper.VisitorMapper;
import com.example.springboot.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;
    @Resource
    private SysMenuService sysMenuService;
    @Resource
    private StudentService studentService;
    @Resource
    private TeacherService teacherService;
    @Resource
    private AdminService adminService;
    @Resource
    private DormManagerService dormManagerService;
    @Resource
    private SysMenuMapper sysMenuMapper;

    //新增
    @Override
    public int addnew(SysRole sysRole) {
        int insert = sysRoleMapper.insert(sysRole);
        return insert;
    }
//删除
    @Override
    public int deleteRole(Integer roleId) {
        int remove=sysRoleMapper.deleteById(roleId);
        return remove;
    }

    @Override
    public Page find(Integer pageNum, Integer pageSize, String search) {
        Page page = new Page<>(pageNum, pageSize);
        QueryWrapper<SysRole> qw = new QueryWrapper<>();
        qw.like("id", search);
        Page rolePage = sysRoleMapper.selectPage(page, qw);
        return rolePage;
    }

    @Override
    public int updateRole(SysRole sysRole) {
        int i = sysRoleMapper.updateById(sysRole);
        return i;
    }


    //角色权限的数据回显
    @Override
    public AssignVo getAssignShow(AssignParm parm) {
       Admin admin = null;
        admin = adminService.getById(parm.getUserId());
        System.out.println(admin);
        List<SysMenu> list = null;
        // 检查用户是否为管理员并获取菜单
        if (admin != null) {
            //超级管理员拥有的菜单
            //构造查询条件
            QueryWrapper<SysMenu> queryWrapper=new QueryWrapper<>();
            queryWrapper.lambda().orderByAsc(SysMenu::getOrderNum);
            list=sysMenuService.list(queryWrapper);
        }else {
            list=sysMenuMapper.getMenuByUserId(parm.getUserId());
        }
        //组装菜单树
        List<SysMenu> menusList=MakeTree.makeMenuTree(list,0L);
        //查询角色原来的菜单权限
        List<SysMenu> menuByRoleId=sysMenuService.getMenuByRoleId(parm.getRoleId());
        //过滤出id
        List<Long> ids=new ArrayList<>();
        Optional.ofNullable(menuByRoleId).orElse(new ArrayList<>())
                .stream()
                .filter(item->item!=null)
                .forEach(item->{
                    ids.add(item.getId());
                });
        //组装返回值
        AssignVo vo=new AssignVo();
        vo.setCheckList(ids.toArray());
        vo.setMenuList(menusList);
        return vo;
    }


}
